import msgboxVue from './index.vue';

// 定义插件对象
const MessageBox = {};
// vue的install方法，用于定义vue插件
MessageBox.install = function (Vue, options) {
  const MessageBoxInstance = Vue.extend(msgboxVue);
  let currentMsg;
  const initInstance = () => {
    // 实例化vue实例
    currentMsg = new MessageBoxInstance();
    let msgBoxEl = currentMsg.$mount().$el;
    document.body.appendChild(msgBoxEl);
  };
  // 在Vue的原型上添加实例方法，以全局调用
  Vue.prototype.$msgBox = {
    showMsgBox (options) {
      if (!currentMsg) {
        initInstance();
      }
      if (typeof options === 'string') {
        currentMsg.content = options;
      } else if (typeof options === 'object') {
        Object.assign(currentMsg, options);
      }
      return currentMsg.showMsgBox()
			.then(val => {
				currentMsg = null;
				return Promise.resolve(val);
			})
			.catch(err => {
				currentMsg = null;
				return Promise.reject(err);
			});
    }
  };
};
export default MessageBox;




/** 
 *  ### 使用方法
 * 在main.js注册全局组件
		import MessageBox from './components/MessageBox/index';
		Vue.use(MessageBox);
 * 
 * 调用
 * 
		this.$msgBox.showMsgBox({
			title: '标题',
			content: '提示内容',
			isShowInput: true  //是否需要显示输入框
		}).then((val) => {
			console.log('点了确定');
		}).catch(() => {
			console.log('点了取消');
		}); 
 */